<!-- 添加用户 -->
<template>
	<div>
		<el-form :model="editFrom" ref="editFrom" :rules="rules" >
			<el-form-item label="用户名" prop="username">
				<el-input v-model="editFrom.username" placeholder="请输入用户名" size="large"></el-input>
			</el-form-item>
			<el-form-item label="密码:" prop="password" v-if="show">
				<el-input v-model="editFrom.password" placeholder="请输入密码" size="large"></el-input>
			</el-form-item>
			<el-form-item label="状态:" prop="flag">
				<el-select v-model="editFrom.flag" placeholder="请选择账户状态" style="width: 100%;" size="large">
				    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
				  </el-select>
			</el-form-item>
		</el-form>
		<div class="dialog-footer">
			<el-button @click="closedialog">取 消</el-button>
			<el-button type="primary" @click="saveEdit">确 定</el-button>
		</div>
	</div>
</template>

<script>
import { } from '@/api/index';

export default {
	name: 'editStudent',
	props: {
		Infoitem: Object
	},
	data() {
	    return {
	        show:true,
			editFrom:{},
			rules: {
				username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
				password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
				flag: [{ required: true, message: '请选择账户状态', trigger: 'blur' }],
			},
			options:[{
				  value: 1,
				  label: '启用',
			},{
				  value: 0,
				  label: '禁用',
			}],
	    };
	},
	mounted() {
		if(this.Infoitem.id){
			this.show = false
			this.getDetil()
		}
		this.editFrom.type = this.Infoitem.type
	},
	methods:{
		getDetil(){
			var parems = {
				id:this.Infoitem.id
			}
			getUserByIdAPI(parems).then(res => {
				if (res.code == 200) {
					this.editFrom = res.data
				} 
			});
		},
		saveEdit(){
			this.$refs.editFrom.validate(valid => {
			    if (valid) {
					if(this.editFrom.id){
						editUserAPI(this.editFrom).then(res => {
							if(res.code == 200){
								this.$message.success(`修改成功`);
								this.editVisible = false;
								this.closedialog();
							}else{
								this.$message.error(res.msg);
							}
						});
					}else{
						var parems = this.editFrom 
						editUserAPI(parems).then(res => {
							if(res.code == 200){
								this.$message.success(`添加成功`);
								this.editVisible = false;
								this.closedialog();
							}else{
								this.$message.error(res.msg);
							}
						});
					}
			    } else {
			        console.log('error submit!!');
			        return false;
			    }
			});
		},
		closedialog() {
			this.$emit('closeDialog', false);
		}
	}
};
</script>

<style scoped>
.dialog-footer {
	width: 100%;
	text-align: right;
}
/deep/.el-col-12{
	padding: 0 15px;
}
</style>
